//
//  EmotionKeyboard.m
//  微博2期
//
//  Created by J on 15/12/13.
//
//

#import "EmotionListView.h"
#import "EmotionTabBar.h"
#import "EmotionKeyboard.h"
#import "Emotion.h"
#import "MJExtension.h"//字典转模型
#import "EmotionTool.h"

@interface EmotionKeyboard ()<EmotionTabBarDelegate>

/**
 *  正在显示的listView
 */
@property(nonatomic,weak)EmotionListView* showingListView;

/**
 *  容纳表情内容的控件
 */
//@property(nonatomic,weak)UIView * contentView;


//1. 表情内容
@property(nonatomic,strong)EmotionListView * recentListView;
@property(nonatomic,strong)EmotionListView * defaultListView;
@property(nonatomic,strong)EmotionListView * emojiListView;
@property(nonatomic,strong)EmotionListView * lxhListView;

//2.tabbar
@property(nonatomic,weak)EmotionTabBar * tabbar;

@end

@implementation EmotionKeyboard
#pragma mark --懒加载
- (EmotionListView *)recentListView{
    
    if (!_recentListView) {
        self.recentListView = [[EmotionListView alloc]init];
        //加载沙盒中数据
//        self.recentListView.emotions = [EmotionTool recentEmotions];
    }
    
    return _recentListView;
    
}



- (EmotionListView *)defaultListView{
    
    if (!_defaultListView) {
        
        self.defaultListView = [[EmotionListView alloc]init];
        
              self.defaultListView.emotions =[EmotionTool defaultEmotions];

        
    }
    
    return _defaultListView;
    
}



- (EmotionListView *)emojiListView{
    
    if (!_emojiListView) {
        
        self.emojiListView = [[EmotionListView alloc]init];
             self.emojiListView.emotions = [EmotionTool emojiEmotions];

        
        
    }
    
    return _emojiListView;
    
}


- (EmotionListView *)lxhListView{
    
    if (!_lxhListView) {
        
        self.lxhListView = [[EmotionListView alloc]init];
              self.lxhListView.emotions = [EmotionTool lxhEmotions];        
    }
    
    return _lxhListView;
    
}

#pragma mark --初始化

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
       //1. contentView
//        UIView * contentView = [[UIView alloc]init];
//        [self addSubview:contentView];
//        self.contentView = contentView;
//        listView.backgroundColor = RamdomColor;
        //2.tabbar
        EmotionTabBar * tabbar = [[EmotionTabBar alloc]init];
        tabbar.delegate = self;
        [self addSubview:tabbar];
        self.tabbar = tabbar;
//        tabbar.backgroundColor = RamdomColor;
        //表情选中的通知
        
        [NoficationCenter addObserver:self selector:@selector(EmotionDidSelect:) name:EmotionDidSelectNotification object:nil];
        
        
        
    }
    
    return self;
}
- (void)EmotionDidSelect:(NSNotification*)noti{
    self.recentListView.emotions = [EmotionTool recentEmotions];//点击表情刷新表情界面的表情
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
    
    //2.tabbar
    
    self.tabbar.height = 37;
    self.tabbar.x = 0;
    self.tabbar.y = self.height - self.tabbar.height;
    self.tabbar.width = self.width;
    

    
    //1. 表情内容
//
    self.showingListView.x = self.showingListView.y = 0;
    self.showingListView.width = self.width;
    self.showingListView.height = self.tabbar.y;//高度为底部的y
//    self.showingListView.backgroundColor = RamdomColor;
//    //3.取出刚才加进去的控件
//    UIView* child = [self.contentView.subviews lastObject];
//    child.frame = self.contentView.bounds;
//    
//    child.backgroundColor = RamdomColor;
//    NSLog(@"%@",self.contentView.subviews);
    
    
}


#pragma mark --EmotionTabBarDelegate
- (void)emotionTabBar:(EmotionTabBar *)tabBar didSelectButton:(EmotionTabBarButtonType)buttonType{
    
    //移除contentView之前显示的控件
//    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   
    [self.showingListView removeFromSuperview];
//    NSLog(@"%@",[self.subviews lastObject]);
    //根据按钮类型 切换contentView上面的View
    switch (buttonType) {
        case EmotionTabBarButtonTypeRecent://最近
        {
            [self addSubview:self.recentListView];
//            self.showingListView = self.recentListView;

        }
            break;
        case EmotionTabBarButtonTypeDefault://默认
        {
            [self addSubview:self.defaultListView];
//            self.showingListView = self.defaultListView;

        }
            break;

        case EmotionTabBarButtonTypeEmoji://表情
        {
            [self addSubview:self.emojiListView];
//            self.showingListView = self.emojiListView;

        }
            break;

        case EmotionTabBarButtonTypeLxh://浪小花
        {
            [self addSubview:self.lxhListView];
//            self.showingListView = self.lxhListView;

        }
            break;

        default:
            break;
    }
    
    self.showingListView = [self.subviews lastObject];//加在最后的肯定是刚才加上去的
    //重新计算子控件frame (setNeedsLayout内部会在恰当的时刻, 重新调用layoutSubviews,重新布局)
    [self setNeedsLayout];
   
    
}
@end
